//
//  MYGlobal.m
//  PetExchange
//
//  Created by a on 2020/12/17.
//

#import "MYGlobal.h"
//水印存储路径
#define shuiyinPath [NSString stringWithFormat:@"%@/Documents/init.txt",NSHomeDirectory()]
@implementation MYGlobal
+(MYGlobal *)share{
    static MYGlobal *sharedGlobalInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedGlobalInstance = [[self alloc] init];
    });
    return sharedGlobalInstance;
}

-(id)init{
    self=[super init];
    if (self) {
        self.wechatFlag = 0;
        self.gwIP = @"";
        self.vip_expiry = [DWKeychainTool getItemForKey:@"vip"];
    }
    return self;
}

-(NSString *)getdayAndWeek:(NSString *)type{
    if(type){
        NSString *day = [Helper getCurrentTimes:@"yyyy.MM.dd"];
        NSString *week = [self getWeekDay:[NSDate new].weekday];
        NSString *time = [Helper getCurrentTimes:@"HH:mm"];
        NSString *times = [Helper getCurrentTimes:@"HH:mm:ss"];
        if([type isEqualToString:@"ABC"]){
            return [NSString stringWithFormat:@"%@ %@ %@",day,week,time];
        }else if([type isEqualToString:@"ACs"]){
            return [NSString stringWithFormat:@"%@ %@",day,times];
        }else if([type isEqualToString:@"AC"]){
            return [NSString stringWithFormat:@"%@ %@",day,time];
        }else if([type isEqualToString:@"AB"]){
            return [NSString stringWithFormat:@"%@ %@",day,week];
        }else if([type isEqualToString:@"BC"]){
            return [NSString stringWithFormat:@"%@ %@",week,time];
        }else if([type isEqualToString:@"A"]){
            return [NSString stringWithFormat:@"%@",day];
        }else if([type isEqualToString:@"B"]){
            return [NSString stringWithFormat:@"%@",week];
        }else if([type isEqualToString:@"C"]){
            return [NSString stringWithFormat:@"%@",time];
        }
    }else{
        return [NSString stringWithFormat:@"%@ %@",[Helper getCurrentTimes:@"yyyy.MM.dd"],[self getWeekDay:[NSDate new].weekday]];
    }
    return  nil;
}

-(NSString *)getWeekDay:(NSInteger)index{
    if(index == 0){
        return  LocalizedString(@"星期日");
    }else if(index == 1){
        return LocalizedString(@"星期一");
    }else if(index == 2){
        return LocalizedString(@"星期二");
    }else if(index == 3){
        return LocalizedString(@"星期三");
    }else if(index == 4){
        return LocalizedString(@"星期四");
    }else if(index == 5){
        return LocalizedString(@"星期五");
    }else if(index == 6){
        return LocalizedString(@"星期六");
    }
    return LocalizedString(@"未知");
}

- (void)saveData{
    
}
//保存数据
-(void)saveDataWithDict:(NSDictionary *)dict  withPath:(NSString *)path{
    //DLog(@"dict===%@",dict);
    if (dict == nil) {
        return;
    }
    NSData *resultData= [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    NSFileManager *fm=[NSFileManager defaultManager];
    if (![fm fileExistsAtPath:path]) {
        [fm createFileAtPath:path contents:nil attributes:nil];
    }
    [resultData writeToFile:path atomically:YES];
}
//得到数据
-(NSMutableDictionary *)getDataWithPath:(NSString *)path{
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSMutableDictionary *dict;
    if (data) {
        dict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:NSJSONReadingMutableContainers error:nil];
    }else{
        dict = [[NSMutableDictionary alloc] init];
    }
    return dict;
}

- (BOOL)isVip{
    NSString *vip_expiry = [DWKeychainTool getItemForKey:@"vip"];
    if(vip_expiry == nil  || vip_expiry.length < 2){
        return NO;
    }
    if([Helper validateWithStartTime:@"2022-11-09 17:00:00" withExpireTime:vip_expiry]){
        return YES;
    }
    return NO;
}

- (void)setVip_expiry:(NSString *)vip_expiry{
    _vip_expiry = vip_expiry;
    [DWKeychainTool saveItem:_vip_expiry key:@"vip"];
}

@end
